package com.ants.boot.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import com.google.common.base.Optional;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author 蚂蚁会花呗
 * @date 2022/2/7 14:28
 * swagger2 配置,支持扫描多个controller 包
 * /ants-boot-local/doc.html
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig{

    // 定义分隔符
    private static final String split = ";";

    @Bean
    public Docket createRestApi() {
        String basePackage = "com.ants.boot.system.controller;com.ants.boot.wx.controller";
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    public static Predicate<RequestHandler> basePackage(final String basePackage){
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }


    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage.split(split)) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }


    private static Optional<Class<?>> declaringClass(RequestHandler input){
        return Optional.fromNullable(input.declaringClass());
    }


    @Bean
    public Docket webApiWx() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfoWx())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/wx/**"))
                .build()
                .groupName("微信相关")
                .pathMapping("/");
    }

    @Bean
    public Docket webApiSystem() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/system/**"))
                .build()
                .groupName("系统相关")
                .pathMapping("/");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("蚂蚁会花呗").description("系统相关")
                .build();
    }


    public ApiInfo apiInfoWx() {
        return  new ApiInfoBuilder()
                .title("蚂蚁会花呗")
                .description("微信相关的api接口")
                .build();
    }

}
